<template>
  <!-- 农场管理 -->
  <BaseMainSlot>
    <template v-slot:zhactbar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-edit="false"
        @add="add"
        @cancel="cancel"
        @del="del"
        @save="save"
      />
    </template>
    <template v-slot:tablebox>
      <el-table
        v-loading="false"
        highlight-current-row
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="dataItemName" label="农场名称" />
        <el-table-column prop="dataItemName" label="农场主" />
        <el-table-column prop="dataItemName" label="联系电话" />
        <el-table-column prop="dataItemName" label="农场地址" />
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:form>
      <div class="zhtabletitle">所有农业单位</div>
    </template>
  </BaseMainSlot>
</template>
<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/basic-info-manage/iot-unit-management";
import BaseMainSlot from "@/components/BaseMainSlot/shangxia.vue"
import { mymixin } from "@/utils/mixin"
import { getToken } from '@/utils/auth';
export default {
  name: "AgriculturalTypeManage",
  components: { ActionBar, Pagination, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      // ----表格二号----
      loadingtwo: true,
      // 当前页
      currentPagetwo: 1,
      // 每页的数据条数
      pageSizetwo: 10,
      // 总条数
      totalSizetwo: 0,
      // 表格二号数据
      tableDatatwo: [],
      // 是否能输入
      // -------
      canInput: false,
      rules: {},
      // 选中的数据项数组 employeeId组成 (此数组最多为一个)
      nowselectedList: []
    };
  },
  watch: {},
  created() {
    this.tableData = [
      { dataItemName: "一号农场" },
      { dataItemName: "二号农场" },
      { dataItemName: "三号农场" }
    ]
    // this.getTableDataListone()
    // 获取表格数据
    // this.getTableDataListtwo();
  },
  mounted() {},
  methods: {
    // 获取当前拥有类型表格
    getTableDataListone() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize
      }
      nowpagereq.ownUnitlist(params).then(res => {
        this.loading = false;
        if (res.code === 0) {
          this.$message.error('当前农业类型表格获取失败')
        }
        let data = res.data.content;
        // 赋值总条数
        this.totalSize = data.totalElements;
        // 赋值数组
        this.tableData = data;
        this.addblankmet();
        this.resetActionBar();
        this.addActive = false
      });
    },
    // 获取所有农业类型表格
    getTableDataListtwo() {
      this.loadingtwo = true;
      let params = {
        page: this.currentPagetwo,
        size: this.pageSizetwo
      }
      nowpagereq.getbaseUnitList(params).then(res => {
        this.loadingtwo = false;
        if (res.code === 0) {
          this.$message.error('所有农业类型表格获取失败')
        }
        let data = res.data;
        // 赋值总条数
        this.totalSizetwo = data.total;
        // 赋值数组
        this.tableDatatwo = data.list;
        if (this.tableDatatwo.length < this.pageSizetwo) {
          let havline = this.tableDatatwo.length % this.pageSizetwo
          if (havline < this.pageSizetwo) {
            const remain = this.pageSizetwo - havline;
            for (var i = 0; i < remain; i++) {
              this.tableDatatwo.push({ 'index': i + this.tableDatatwo.length - 1 });
            }
          }
        }
        this.resetActionBar();
        this.addActive = false
      });
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    changePageSizetwo(pageSize) {
      this.pageSizetwo = pageSize;
      this.getTableDataListtwo();
    },
    changeCurrentPagetwo(currentPage) {
      this.currentPagetwo = currentPage;
      this.getTableDataListtwo();
    },
    handleSelectionChange(list) {
      this.nowselectedList = [];
      this.nowselectedList = list.map(item => {
        return item.ownUnitId
      });
      if (this.nowselectedList && this.nowselectedList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    handleSelectionChangetwo(list) {
      this.nowselectedList = [];
      let id = getToken('companyId')
      this.nowselectedList = list.map(item => {
        return {
          'description': item.description,
          'unitId': item.unitId,
          'unitName': item.unitName,
          'symbol': item.symbol,
          'companyId': id }
      });
      if (this.nowselectedList && this.nowselectedList.length > 0) this.addActive = true;
      else this.addActive = false;
    },
    delmessage() {
      if (this.nowChanceStau === 'del') {
        nowpagereq.delUnitlist(this.nowselectedList).then(
          res => {
            if (res.code === 1) {
              this.getTableDataListone()
              this.$message.success('删除成功')
            } else {
              this.$message.error('删除失败')
            }
          }
        )
      }
    },
    save() {
      if (this.nowChanceStau === 'add') {
        nowpagereq.addUnitlist(this.nowselectedList).then(
          res => {
            if (res.code === 1) {
              this.getTableDataListone()
              this.$message.success('添加成功')
            } else {
              this.$message.error('添加失败')
            }
          }
        )
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.zhtabletitle{
  color:#31f7dd;
  margin-bottom: 10px;
}
</style>
